package com.andreamerla.petslife.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.andreamerla.petslife.R;
import com.andreamerla.petslife.manager.AlertDialogManager;
import com.andreamerla.petslife.util.Utils;

/**
 * Created by IntelliJ IDEA.
 * User: a.merla
 * Date: 15/07/13
 * Time: 15.55
 */
public class CiboCompletoActivity extends ActionBarActivity implements View.OnClickListener {
    private EditText etHumidity;
    private EditText etProtein;
    private EditText etFat;
    private EditText etAsh;
    private EditText etFiber;
    private Button btCalculate;
    private Button btClear;
    private View llResults;
    private TextView tvResultProtein;
    private TextView tvResultFat;
    private TextView tvResultAsh;
    private TextView tvResultFiber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cibo_completo_layout);

        etHumidity = (EditText) findViewById(R.id.et_humidity);
        etProtein = (EditText) findViewById(R.id.et_protein);
        etFat = (EditText) findViewById(R.id.et_fat);
        etAsh = (EditText) findViewById(R.id.et_ash);
        etFiber = (EditText) findViewById(R.id.et_fiber);

        btCalculate = (Button) findViewById(R.id.bt_calculate);
        btCalculate.setOnClickListener(this);
        btClear = (Button) findViewById(R.id.bt_clear);
        btClear.setOnClickListener(this);

        llResults = findViewById(R.id.ll_results);
        tvResultProtein = (TextView) findViewById(R.id.tv_result_protein);
        tvResultFat = (TextView) findViewById(R.id.tv_result_fat);
        tvResultAsh = (TextView) findViewById(R.id.tv_result_ash);
        tvResultFiber = (TextView) findViewById(R.id.tv_result_fiber);
    }

    public void onClick(View view) {
        if (view == btCalculate) {
            calculate();
            Utils.hideKeyboard(this, view);
        } else if (view == btClear) {
            clear();
            Utils.hideKeyboard(this, view);
        }
    }

    private void calculate() {
        try {
            float humidity = Float.parseFloat(etHumidity.getText().toString());
            float protein = Float.parseFloat(etProtein.getText().toString());
            float fat = Float.parseFloat(etFat.getText().toString());
            float ash = Float.parseFloat(etAsh.getText().toString());
            float fiber = Float.parseFloat(etFiber.getText().toString());
            float sum = humidity + protein + fat + ash + fiber;

            if (sum > 100) {
                AlertDialogManager.showAlertDialog(this, getString(R.string.error), getString(R.string.error_sum_over_one));
                return;
            }

            protein = protein / (100 - humidity) * 100;
            fat = fat / (100 - humidity) * 100;
            ash = ash / (100 - humidity) * 100;
            fiber = fiber / (100 - humidity) * 100;

            protein = Utils.round(protein, 1);
            fat = Utils.round(fat, 1);
            ash = Utils.round(ash, 1);
            fiber = Utils.round(fiber, 1);

            tvResultProtein.setText(protein + getString(R.string.percent));
            tvResultFat.setText(fat + getString(R.string.percent));
            tvResultAsh.setText(ash + getString(R.string.percent));
            tvResultFiber.setText(fiber + getString(R.string.percent));
            llResults.setVisibility(View.VISIBLE);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            AlertDialogManager.showAlertDialog(this, getString(R.string.error), getString(R.string.error_invalid_data));
        }
    }

    private void clear() {
        etHumidity.setText("");
        etProtein.setText("");
        etFat.setText("");
        etAsh.setText("");
        etFiber.setText("");
        llResults.setVisibility(View.GONE);

        etHumidity.requestFocus();
    }
}
